/*
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef CHAT_CTRL_H
#define CHAT_CTRL_H

#include "atlstr.h"
//[-]PPA [Doxygen 1.5.1]  #include "TypedListViewCtrl.h"
#include "ImageDataObject.h"
#include "UserInfo.h"

#define WM_UPDATE_SMILE WM_USER + 1

#ifndef _RICHEDIT_VER
# define _RICHEDIT_VER 0x0300
#endif

#include <AtlCrack.h>

class UserInfo;
typedef list<pair<int, tstring>> TURLMap;

class ChatCtrl: public CWindowImpl<ChatCtrl, CRichEditCtrl>, public IRichEditOleCallback
{
		typedef ChatCtrl thisClass;
	protected:
		// TypedListViewCtrl<UserInfo, IDC_USERS> *m_pUsers;
		string clientUrl; // !SMT!-S
		bool isOnline(const tstring& aNick); // !SMT!-S
		
		bool m_boAutoScroll;
		
		IStorage *m_pStorage;
		IRichEditOle* m_pRichEditOle;
		LPLOCKBYTES m_lpLockBytes;
	public:
		ChatCtrl();
		virtual ~ChatCtrl();
		
		BEGIN_MSG_MAP(thisClass)
		MESSAGE_HANDLER(WM_UPDATE_SMILE, onUpdateSmile)
		MESSAGE_HANDLER(WM_MOUSEWHEEL, onMouseWheel)
		MESSAGE_HANDLER(WM_SIZE, onSize)
		END_MSG_MAP()
		
		LRESULT onUpdateSmile(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/);
		LRESULT onMouseWheel(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/);
		LRESULT onSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/);
		LRESULT onMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/);
		
		void Initialize();
		
		LRESULT OnRButtonDown(POINT pt);
		
		bool HitNick(const POINT& p, tstring& sNick, int& piBegin , int& piEnd);
		bool HitIP(const POINT& p, tstring&  sIP, int& piBegin, int& piEnd);
		bool HitURL();
		
		tstring LineFromPos(const POINT& p) const;
		
		void AdjustTextSize();
		void AppendText(const Identity& i, const tstring& sMyNick, const tstring& sTime, const LPCTSTR sMsg, CHARFORMAT2& cf, bool bUseEmo = true);
		void AppendTextOnly(const tstring& sMyNick, const LPCTSTR sMsg, CHARFORMAT2& cf, bool bMyMess, const tstring& sAuthor, bool isBot);
		
		void GoToEnd();
		bool GetAutoScroll() const
		{
			return m_boAutoScroll;
		}
		void SetAutoScroll(bool boAutoScroll);
		//void SetUsers(TypedListViewCtrl<UserInfo, IDC_USERS> *pUsers);
		void SetClientUrl(const string& url)
		{
			clientUrl = url;    // !SMT!-S
		}
		void SetTextStyleMyNick(CHARFORMAT2 ts)
		{
			WinUtil::m_TextStyleMyNick = ts;
		}
		LRESULT OnNotify(int idCtrl, ENLINK pnmh);
		
		static tstring sSelectedLine;
		static tstring sSelectedIP;
		static tstring sTempSelectedUser;
		TURLMap lURLMap;
		tstring get_URL(ENLINK* p_EL) const;
		
		GETSET(Client*, client, Client);
		
	protected:
	
		volatile LONG m_Ref;
		// IRichEditOleCallback implementation
		
		COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE QueryInterface(THIS_ REFIID riid, LPVOID FAR * lplpObj);
		COM_DECLSPEC_NOTHROW ULONG STDMETHODCALLTYPE AddRef(THIS);
		COM_DECLSPEC_NOTHROW ULONG STDMETHODCALLTYPE Release(THIS);
		
		// *** IRichEditOleCallback methods ***
		COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE GetNewStorage(THIS_ LPSTORAGE FAR * lplpstg);
		COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE GetInPlaceContext(THIS_ LPOLEINPLACEFRAME FAR * lplpFrame,
		                                                                 LPOLEINPLACEUIWINDOW FAR * lplpDoc,
		                                                                 LPOLEINPLACEFRAMEINFO lpFrameInfo);
		COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE ShowContainerUI(THIS_ BOOL fShow);
		COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE QueryInsertObject(THIS_ LPCLSID lpclsid, LPSTORAGE lpstg,
		                                                                 LONG cp);
		COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE DeleteObject(THIS_ LPOLEOBJECT lpoleobj);
		COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE QueryAcceptData(THIS_ LPDATAOBJECT lpdataobj,
		                                                               CLIPFORMAT FAR * lpcfFormat, DWORD reco,
		                                                               BOOL fReally, HGLOBAL hMetaPict);
		COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE ContextSensitiveHelp(THIS_ BOOL fEnterMode);
		COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE GetClipboardData(THIS_ CHARRANGE FAR * lpchrg, DWORD reco,
		                                                                LPDATAOBJECT FAR * lplpdataobj);
		COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE GetDragDropEffect(THIS_ BOOL fDrag, DWORD grfKeyState,
		                                                                 LPDWORD pdwEffect);
		COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE GetContextMenu(THIS_ WORD seltype, LPOLEOBJECT lpoleobj,
		                                                              CHARRANGE FAR * lpchrg,
		                                                              HMENU FAR * lphmenu);
};


#endif //!defined(AFX_CHAT_CTRL_H__595F1372_081B_11D1_890D_00A0244AB9FD__INCLUDED_)
